package alo.p4.work;

import java.util.Stack;
//对栈中的元素进行升序排序
//类似以汉诺塔的方法
public class StackSorter {
    public Stack sort(Stack stack){
        Stack r=new Stack();
        while (!stack.isEmpty()) {
            int temp = (int) stack.pop();
            //降序移动到stack
            while (!r.isEmpty() && (int)r.peek() > temp) {
                stack.push(r.pop());
            }
            //升序移动到r
            r.push(temp);
        }
        return r;
    }
}
